package com.bpvse;


import com.util.CommandGeneratorUnit;
import com.util.F1Unit;
import com.util.bytesToHexUnit;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Base64;

@Service
public class TrapdoorService {

    private static final int HASH_LEN = 32; // 假设 HASH_LEN 是 32
    byte[] TrapadoorMessage = new byte[HASH_LEN];

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    //l是最新版本号
    public void generateTrapdoor(byte[] trapdoor, byte[] ckey, int l, byte[] keyword, int wordLen) {
        String str;
        byte[] v1 = new byte[HASH_LEN];

        try {
            String reply = stringRedisTemplate.opsForValue().get(String.valueOf(l));  //获得对应版本对应的v1
            System.out.println("版本l对应的状态是"+reply);

            if (reply != null) {
                v1 = Base64.getDecoder().decode(reply);
                System.out.println("v1的内容是"+Arrays.toString(v1));
                System.out.println("v1的加密内容是:"+Base64.getEncoder().encodeToString(v1));
            } else {
                System.out.println("version error!");
                return;
            }
            F1Unit.F1(trapdoor, keyword, wordLen, v1, ckey, 1);  //通过v1查找st
            System.arraycopy(trapdoor, 0, TrapadoorMessage, 0, trapdoor.length);
            //这个陷门的内容就是获得其最新版本的状态
            System.out.println("trapdoor的内容：" + Arrays.toString(trapdoor));
            System.out.println("trapdoor的加密内容是:" + Base64.getEncoder().encodeToString(trapdoor));

        } catch (Exception e) {
            System.out.println("connection error: " + e.getMessage());
        }
    }

}
